var app = getApp()
var valid = require('../../../utils/valid')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    addId:0,
    is_default:0,
    region: '请选择',
    hasLocation: false,
    select_items: '',

  },

  //  选择地区
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if(typeof(options.id) != 'undefined'){
      wx.request({
        url: app.url + 'api_user/addressItemV1',
        data: {
          id: options.id,
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        }, success: function (json) {
          if (json.data.done) {
            var result = json.data.result
            var region = [result.province_name ,  result.city_name ,  result.area_name ]
            that.setData({ 'data': result, 'region': region, 'addId': result.id, 'is_default': result.is_default})

          } else {
            wx.showModal({
              title: '提示',
              content: json.data.message
            })

          }
        }
      })
    }
  },

  // 选择详细地址
  chooseLocation: function (e) {
    console.log(e)
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        // success
        console.log(res)
        that.setData({
          hasLocation: true,
          location: {
            longitude: res.longitude,
            latitude: res.latitude,
            name: res.name
          }
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },

  // 验证
  openLocation: function (e) {
    var that = this;
    var items = that.data.items;
    if (e.detail.value.persion == '') {
      prompt('请输入姓名', 'loading')
      return false;
    }
    if (valid.mobile(e.detail.value.mobile) == false) {
      wx.showToast({
        title: '号码格式不正确',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return false
    }
    if (this.data.region == '请选择') {
      prompt('请选地区', 'loading')
      return false;
    }
    if (e.detail.value.address == '') {
      prompt('请输入地址', 'loading')
      return false;
    }
    // if (valid.postalCode(e.detail.value.zip) == false) {
    //   wx.showToast({
    //     title: '邮编格式不正确',
    //     icon: 'loading',
    //     duration: 1000,
    //     mask: true
    //   })
    //   return false
    // }
    
    wx.showModal({
      title: '提示',
      content: '请问您确认提交吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: app.url + 'api_user/addressEditV1',
            data: { 
              accept_name: e.detail.value.persion,
              province: that.data.region[0],
              city: that.data.region[1],
              area: that.data.region[2],
              address: e.detail.value.address,
              zip: e.detail.value.zip,
              mobile: e.detail.value.mobile,
              id:that.data.addId,
              is_default:that.data.is_default
            },
            header: { 
              'content-type': 'application/json',
              'token': wx.getStorageSync('token_key') 
            },
      success: function (res) {
        if (res.data.done) {
          prompt('提交成功！', 'success');
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2]; //上一个页面
          prevPage.setData({
            is_order:prevPage.data.is_order,
            on_use:prevPage.data.on_use,
            is_edit:1
          }) 

           setTimeout(function () { wx.navigateBack() }, 1200);//返回上一个页面
        } else {
          prompt(res.data.message, 'loading');
        }
      },
          })
        }
    }
    })
  },
})
function prompt(text, stat) {
  //提示信息
  wx.showToast({
    title: text,
    icon: stat,
    duration: 1200,
  })
};